package com.gemme.gemme.wode;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alipay.sdk.app.AuthTask;
import com.alipay.sdk.app.PayTask;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.OrderInfoUtil2_0;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//侧滑删除地址
public class TestAlipayActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_ali);
        initTop();
        test();
    }

    private void test() {
        PayTask payTask = new PayTask(this);
        String version = payTask.getVersion();
        System.out.println("支付宝 version : " + version);
        getjimeiali();

    }

    private void getjimeiali() {
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();
        if(TextUtils.isEmpty(access_token))
            return;
        String url = DataManager.BaseUrl + "user/thirdaccount/aliauth?type=ALI";
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);

        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {
                try {
                    JiMeiBean jiMeiBean = JSON.parseObject(response, JiMeiBean.class);
                    System.out.println("");
                    PID = jiMeiBean.getData().getPid();
                    APPID = jiMeiBean.getData().getAppid();
                    RSA2_PRIVATE = jiMeiBean.getData().getRsa2_PRIVATE();
                    shouquan();
                // shouquan();
                }catch (Exception e){

                }

            }
        });
    }

    String PID = "";//"2088831884239985"
    String APPID = "";//2021001170600377
    String RSA2_PRIVATE = "";

    private void shouquan() {
        // authInfo 的构造方式详见 授权请求参数 一节，或参考支付宝 SDK Demo 中的实现。
// authInfo 的生成包括签名逻辑。故生成过程请务必在服务端进行。
        if(TextUtils.isEmpty(PID) ||
                TextUtils.isEmpty(APPID) ||
                TextUtils.isEmpty(RSA2_PRIVATE)){
            return;
        }
        String TARGET_ID = System.currentTimeMillis() + "";
        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> authInfoMap = OrderInfoUtil2_0.buildAuthInfoMap(PID, APPID, TARGET_ID, rsa2);
        String info = OrderInfoUtil2_0.buildOrderParam(authInfoMap);

//        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
        String privateKey = RSA2_PRIVATE;
        String sign = OrderInfoUtil2_0.getSign(authInfoMap, privateKey, rsa2);
        final String authInfo = info + "&" + sign;
        Runnable authRunnable = new Runnable() {

            @Override
            public void run() {
                // 构造AuthTask 对象
                AuthTask authTask = new AuthTask(TestAlipayActivity.this);
                // 调用授权接口，获取授权结果
                Map<String, String> result = authTask.authV2(authInfo, true);
                System.out.println("");
               /* Message msg = new Message();
                msg.what = SDK_AUTH_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);*/
            }
        };

        // 必须异步调用
        Thread authThread = new Thread(authRunnable);
        authThread.start();
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("支付宝测试");
    }




}